home *** CD-ROM | disk | FTP | other *** search
/ boe.pres.k12.wv.us / boe.pres.k12.wv.us.zip / boe.pres.k12.wv.us / Utilities / Xerox Workcentre 5335 / Windows Scan / 32-bit_x86 / Italiano / cpsimage.cab / data / xipProcs / thresholdRange.proc < prev    next >
Text File  |  2009-03-16  |  7KB  |  200 lines

  1. // Load scripted support procedures
  2. // #load "xipProcs/printLayer.proc";
  3.  
  4. // Dynamically load necessary functions
  5. LoadClasses (filename: "xeng");
  6.  
  7. /* @XIPThresholdRange
  8.   // DESCRIPTION
  9.   Given an image and a range of intensity values, this procedure thresholds
  10.   each image part of the image. All image parts outside their respective range
  11.   of graylevels are set to the minimum graylevel; and all image parts inside
  12.   their range are set to the maximum graylevel.
  13.   
  14.   This procedure can also be used to perform simple image segmentation. An image
  15.   can be segmented into distinct regions such that all pixels outside the range
  16.   are set to black and the remaining pixels are either left at their original
  17.   intensities or set to white. The resulting image is returned as the XIPIMAGE
  18.   object "dst".
  19.   
  20.   // ARGUMENTS
  21.   XIPIMAGE src        This is the image to threshold. If the image is less than
  22.                       8 bits/sep, the image will be upconverted to 8 bits/sep.
  23.                       If the image is greater than 8 bits/sep but less than
  24.                       16 bits/sep, the image will be upconverted to 16 bits/sep.
  25.                       If the image is greater than 16 bits/sep, the image will
  26.                       be downconverted to 16 bits/sep.
  27.   
  28.   ELFLIST  minLevels  This is the minimum graylevel(s) at which the image will
  29.                       be thresholded. One level may be specified to be used for
  30.                       all image parts, or a separate level for each image part.
  31.                       A graylevel of an image part less than the corresponding
  32.                       minimum level is set to 0. If the number of levels
  33.                       specified is less than the number of image parts, the
  34.                       last specified level will be used for the remaining image
  35.                       parts. A default minimum level of 0 will be used for each
  36.                       image part if this argument is not specified.
  37.   
  38.   ELFLIST  maxLevels  This is the maximum graylevel(s) at which the image will
  39.                       be thresholded. One level may be specified to be used for
  40.                       all image parts, or a separate level for each image part.
  41.                       A graylevel of an image part greater than the
  42.                       corresponding maximum level is set to 0. If the number of
  43.                       levels specified is less than the number of image parts,
  44.                       the last specified level will be used for the remaining
  45.                       image parts. The default maximum level for each image
  46.                       part is half of the maximum value for the bits/sep in the
  47.                       image.
  48.   
  49.   INTEGER  mode       This specifies the threshold range mode. It can be:
  50.                         0 to threshold levels of each image part only
  51.   
  52.                         1 to threshold levels of each image part so that all
  53.                           pixels outside the range are black and the remaining
  54.                           pixels are left at their original intensities
  55.   
  56.                         2 to threshold levels of each image part so that all
  57.                           pixels outside the range are black and the remaining
  58.                           pixels are white
  59.   
  60.   // RETURNS
  61.   XIPIMAGE dst        Thresholded image
  62. */
  63. PROCEDURE XIPThresholdRange (XIPIMAGE src, ELFLIST minLevels, ELFLIST maxLevels, INTEGER mode)
  64.   RETURNS (XIPIMAGE dst)
  65. {
  66.   INTEGER cMinGrayLevel;
  67.   INTEGER cMaxGrayLevel;
  68.   INTEGER cMaxGrayLevelDiv2;
  69.  
  70.   ELFLIST minLevs;
  71.   ELFLIST maxLevs;
  72.   ELFLIST minShift;
  73.   INTEGER k;
  74.   INTEGER nMinLevels;
  75.   INTEGER nMaxLevels;
  76.   INTEGER nseps;
  77.   INTEGER bits;
  78.   INTEGER doShift;
  79.  
  80.   bits = src.getMember (member: "bits");
  81.   if (bits < 8)
  82.     src = src.convert (to: (8));
  83.   else if ((bits > 8) && (bits != 16))
  84.     src = src.convert (to: (16));
  85.  
  86.   if (src.getMember (member: "interleave") != XIP_SCANLINE_INTER)
  87.     src = src.interleave (scanline: 1);
  88.  
  89.   cMinGrayLevel = 0;
  90.   if (bits == 8) {
  91.     cMaxGrayLevel     = 255;
  92.     cMaxGrayLevelDiv2 = 127;
  93.   } else {
  94.     cMaxGrayLevel     = 65535;
  95.     cMaxGrayLevelDiv2 = 32767;
  96.   }
  97.  
  98.   nseps = src.getMember (member: "seps");
  99.  
  100.   nMinLevels = minLevels.length ();  
  101.   if (nMinLevels == 0) {
  102.     for (k=0; k<nseps; k++) {
  103.       minLevs.insert (obj: new (INTEGER, value:cMinGrayLevel),
  104.                       entry: k);
  105.       minShift.insert (obj: new (INTEGER, value:1),
  106.                        entry:k);
  107.     }
  108.   } else {
  109.     for (k=0; k<nseps; k++) {
  110.       if (k < nMinLevels)
  111.         minLevs.insert (obj: new (INTEGER, value:minLevels[k]),
  112.                         entry: k);
  113.       else
  114.         minLevs.insert (obj: new (INTEGER, value:minLevels[nMinLevels-1]),
  115.                         entry: k);
  116.     }
  117.   }
  118.  
  119.   nMaxLevels = maxLevels.length ();
  120.   if (nMaxLevels == 0) {
  121.     for (k=0; k<nseps; k++) {
  122.       maxLevs.insert (obj: new (INTEGER, value:cMaxGrayLevelDiv2),
  123.                       entry: k);
  124.     }
  125.   } else {
  126.     for (k=0; k<nseps; k++) {
  127.       if (k < nMaxLevels)
  128.         maxLevs.insert (obj: new (INTEGER, value:maxLevels[k]),
  129.                         entry: k);
  130.       else
  131.         maxLevs.insert (obj: new (INTEGER, value:maxLevs[nMaxLevels-1]),
  132.                         entry: k);
  133.     }
  134.   }
  135.  
  136.   for (k=0; k<nseps; k++) {
  137.     if (minLevs[k] == 0) {
  138.       minShift.insert (obj: new (INTEGER, value:1), entry: k);
  139.       doShift = 1;
  140.     } else {
  141.       minLevs[k] = minLevs[k]-1;
  142.       minShift.insert (obj: new (INTEGER, value:0), entry: k);
  143.     }
  144.   }
  145.  
  146.   // Threshold using specified range (mode 0)
  147.   if (doShift) {
  148.     dst = src.shifter (levels: minShift
  149.             ).threshold (levels: minLevs
  150.             ).math (input: src.threshold (levels: maxLevs).invert (),
  151.                     operator: "and"
  152.             );
  153.   } else {
  154.     dst = src.threshold (levels: minLevs
  155.             ).math (input: src.threshold (levels: maxLevs).invert (),
  156.                     operator: "and"
  157.             );
  158.   }
  159.  
  160.   // Assign appropriate value to pixels inside range for modes 1 and 2
  161.   if (mode != 0) {
  162.     XIPIMAGE img;
  163.     XIPIMAGE imgSep;
  164.  
  165.     // Combine image parts to produce image where pixels outside range are
  166.     // black and pixels inside range are white (mode 2)
  167.     img = dst;
  168.     dst = img.channel (parts: "1");
  169.  
  170.     for (k=2; k<=nseps; k++) {
  171.       imgSep = img.channel (parts: "" + k);
  172.  
  173.       dst = dst.math (operator: "and", input: imgSep);
  174.     }
  175.  
  176.     // Create image so that pixels outside range are black and pixels inside
  177.     // range are left unchanged (mode 1)
  178.     if (mode == 1) {
  179.       INTEGER photo;
  180.       STRING  photoname;
  181.  
  182.       photo     = src.getMember (member: "photometry");
  183.       photoname = Photo2Str (photo: photo);
  184.  
  185.       if (photoname == "gray") {
  186.         dst = src.math (operator: "and", input: dst);
  187.       } else if (PhotoIsRGB (photo: photo) || PhotoIsGray (photo: photo)) {
  188.         dst = src.math (operator: "and",
  189.                         input: dst.cspace (outspace: photoname, precise: 1));
  190.       } else {
  191.         dst = src.cspace (outspace: "srgb", precise: 1
  192.                 ).math (operator: "and",
  193.                         input: dst.cspace (outspace: "srgb", precise: 1)
  194.                 ).cspace (outspace: photoname, precise: 1
  195.                 );
  196.       }
  197.     }
  198.   }
  199. }
  200.